/*
This file is part of Docking@Home.
http://docking.gcl.cis.udel.edu
Copyright (C) 2009 University of Delaware

Docking@Home is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Docking@Home is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Docking@Home.  If not, see <http://www.gnu.org/licenses/>.
*/
/*
	Filename: HighLevelTab.h
	Description: A tab in our gui that will control and display any 
		of the high details of the protein and ligand simulation.
	Revision History: 
		Created - 01/14/08
	Author: Robert Keller (01/14/08 -> Current)
*/
#include "HighLevelTab.h"

// ------------------------------------------------------------
// Constructor / Destructor
// ------------------------------------------------------------

HighLevelTab::HighLevelTab ()
{
	this->infoScale=.85f;
	this->tempLine=NULL;
	init("");
}// HighLevelTab

HighLevelTab::HighLevelTab (const char n[TAB_MAX_NAME_LENGTH])
{
	this->infoScale=.85f;
	this->tempLine=NULL;
	init(n);
}// HighLevelTab

HighLevelTab::~HighLevelTab ()
{
	//deallocate the mem for the line of text
	if(this->tempLine!=NULL){
		delete []this->tempLine;
	}
}// ~HighLevelTab

// ------------------------------------------------------------
// Initialization Functions
// ------------------------------------------------------------

void HighLevelTab::init (const char n[TAB_MAX_NAME_LENGTH])
{
	//name the tab
	setName(n);
	//here's all the instructions
	string lines="\n3-D View:\n     + or = zoom in\n     - or _ zoom out\n     < or , rotate left\n     > or . rotate right\n     m switch between \'Min Energy\' and \'Min RMSD\' views\n\nLigand and Complex ONLY:\n     1 change to VDW representation\n     2 change to CPK representation\n     3 change to line representation\n\nMouse Buttons:\n     left click select tab or button\n     hold rotate object (only when in model view window)\n\nFull Screen Mode:\n     to run the application in full screen mode, set BOINC as your screensaver\n     D@H will run in full screen mode while your screensaver is on\n";
	//count how many lines there are
	size_t pos;
	//longest line length
	size_t lineLength=0;
	//while we have endlines
	while((pos=lines.find('\n'))!=lines.npos){
		//delete the endline
		lines.erase(pos,1);
		//get the line
		this->instructions.push_back(lines.substr(0,pos));
		//remove it if it's not the last line
		lines=lines.substr(pos);
		//if that line is longer than the longest
		if(lineLength<this->instructions.back().size())
			lineLength=this->instructions.back().size();
	}
	//allocate some mem for copying strings
	this->tempLine=new char[lineLength+1];	
	
}// init

// ------------------------------------------------------------
// Required Tab Functions 
// ------------------------------------------------------------

void HighLevelTab::resizeScales (bool bFullscreen)
{
}// resizeScales

void HighLevelTab::draw (unsigned int index, bool bFullScreen)
{
	switch (state)
	{
	case UNSELECTED:
		drawUnselectedTab(index);
		break;
	case SELECTED:
		drawSelectedTab(index);
		drawTabWindowArea();
		break;
	case HOVER:
		drawHoveredTab(index);
		if (bSelected)
			drawTabWindowArea();
		break;
	}// switch
}// void 

void HighLevelTab::drawSelectedTab (unsigned int index)
{
	// Starting positions of the tab
	float tabStartX;
	float tabStartY;

	// Draw orthographic information

	glHelper->setOrthographic();

	getTabStart (index, &tabStartX, &tabStartY);

	glHelper->drawFilledRectWH(tabStartX,
		tabStartY,
		width, height,
		selectedBoxColor);

	glHelper->drawText(textSelectedAlpha,
		tabStartX + nameIndentX * tabWindow->getWidthFactor(), 
		tabStartY + (TAB_HEIGHT + nameIndentY) * tabWindow->getHeightFactor(),
		0.0, tabScaleTextX, tabScaleTextY, 1.0f, selectedTextColor, 0, name);

	glHelper->setOrthographicDone();

}// drawSelectedTab

void HighLevelTab::drawUnselectedTab (unsigned int index)
{
	// Starting positions of the tab
	float tabStartX;
	float tabStartY;

	glHelper->setOrthographic();

	getTabStart (index, &tabStartX, &tabStartY);

	glHelper->drawRectWH(tabStartX,
		tabStartY,
		width, height,
		unselectedBoxColor);

	glHelper->drawText(textUnselectedAlpha, 
		tabStartX + nameIndentX * tabWindow->getWidthFactor(), 
		tabStartY + (TAB_HEIGHT + nameIndentY) * tabWindow->getHeightFactor(),
		0.0, tabScaleTextX, tabScaleTextY, 1.0f, unselectedTextColor, 0, name);

	glHelper->setOrthographicDone();
}// drawUnselectedTab

void HighLevelTab::drawHoveredTab (unsigned int index)
{
	if (bSelected)
		drawSelectedTab(index);
	else
		drawUnselectedTab(index);

}// drawHoveredTab

void HighLevelTab::drawTabWindowArea ()
{
	//set for 2d drawing
	glHelper->setOrthographic();
	//make the color white
	float color[4];
	glHelper->copyColor4f(color, glHelper->WHITE);
	//for each line of text
	float startY=(tabWindow->getTopLeftY() + nameIndentY) * 1.25f;
	for(size_t i=0;i<this->instructions.size();i++){
		//copy the line
		strcpy(this->tempLine,this->instructions[i].c_str());
		//draw the line
		glHelper->drawText(textUnselectedAlpha,	tabWindow->getTopLeftX() + nameIndentX,	startY,	1.0f, tabScaleTextX*infoScale, tabScaleTextY*infoScale, 1.0f, color, 0, this->tempLine);
		//move down one
		startY-=25*tabScaleTextY*infoScale;
	}
	glHelper->setOrthographicDone();

}// drawTabWindowArea

void HighLevelTab::drawFullScreen ()
{
}// drawFullScreen

void HighLevelTab::handleMouseMove (int x, int y, int left, int middle, int right)
{
}// handleMouseMove

void HighLevelTab::handleMouseButton (int x, int y, int which, int is_down)
{	
}// handleMouseButton

void HighLevelTab::handleKeyPress (int key1, int key2)
{
}// handleKeyPress

void HighLevelTab::handleKeyRelease (int key1, int key2)
{
}// handleKeyRelease
